$(document).ready(function() {
	$('.jquery_date_picker').datetime({
		userLang : 'en',
		dateFormat : DATE_FORMAT_DATETIMEPICKER_DISPLAY,
		hasTime : true,
		americanMode : false,
		showSecond : true,
		showMillisec : false,
		timeFormat : 'h:m:s'
	});
	$('.jquery_datetime_picker').datetime({
		userLang : 'en',
		dateFormat : DATE_FORMAT_DATETIMEPICKER_DISPLAY,
		hasTime : true,
		americanMode : false,
		showSecond : true,
		showMillisec : false,
		timeFormat : 'h:m:s'
	});
	$.validator.addMethod("maxTo", function(value, element,	params) {
		if ($.isArray(params)) {
			return false;
		}
		if($(params).length > 0) {
			var value2 = $(params).val();
			if(value2 == null) {
				return true;
			}
			if ($.isNumeric(value) && $.isNumeric(value2)) {
				return parseFloat(value2) >= parseFloat(value);
			}
			//return new Date(value2) >= value.toDate();
			return value2.toDate() >= value.toDate();
		}
		return true;
	});
	$.validator.addMethod("datetime", function(value, element) {
		var date = (value+'').toDate();
		if(date == null) {
			date = value;
		}
		return $.validator.methods['date'].call(this,date,element);
	});
	$.validator.addMethod("atLeastCheck", function(value, element, params) {
		if ($.isArray(params)) {
			return false;
		}
		var list = $('#' + params + ' [name^="betInfo.betPosInfoList["][name$="].active"]');
		var isChecked = false;
		if (list != null) {
			for ( var i = 0; i < list.length; i++) {
				if (list[i].checked == true) {
					isChecked = true;
				}
			}
		}
		return isChecked;
	});
	$("#frmAddBet_BettingExchange").validate({
		rules : {},
		messages : {}
	// ,
	// submitHandler : function(form) {
	//			
	// }
	});
	$("#frmAddBet_MutualBetting").validate({
		rules : {},
		messages : {}
	// ,
	// submitHandler : function(form) {
	//			
	// }
	});

	$('#imgEvent').click(function() {
		if ($('#_eventId').val() != null
				&& $('#_eventId').val() != '') {
			$('#uploadImage').click();
		}
	});

	$('#uploadImage').change(function() {
		var options = {
			beforeSubmit : function(formData, jqForm, options) { // pre-submit
				// callback
				var queryString = $.param(formData);
				if (queryString == '' || queryString == 'resourceFile=') {
					return false;
				}
				return true;
			},
			success : function(responseText, statusText) {
				_uploadSuccess(responseText, statusText);
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert(xhr.status);
				alert(thrownError);
			},
			url : UPLOAD_FILE_URL,
			type : 'post', 
			dataType : 'json',
			iframe : true
		};
		$('#frmUploadResource').ajaxSubmit(
				options);
	});
	
	$('form[id^="frmAddBet_"]').each(function() {
		var name = $(this).attr('name');
		$('#' + name + ' [name="betInfo.name"]').rules("add", {
			required : true,
			maxlength : 255,
			messages : {
				required : message_bet_name_required,
				maxlength : message_bet_name_maxlength
			}
		});
		$('#' + name + ' [name="betInfo.description"]').rules("add", {
			maxlength : 256,
			messages : {
				maxlength : message_bet_description_maxlength
			}
		});
		if($('#' + name + ' [name="betInfo.amount"]').length > 0) {
			$('#' + name + ' [name="betInfo.amount"]').rules("add", {
				required : true,
				number : true,
				maxlength : 15,
				min : 0,
				messages : {
					required : message_bet_housestake_required,
					number : message_bet_housestake_number,
					maxlength : message_bet_housestake_maxlength,
					min : message_bet_housestake_min
				}
			});
		}
		$('#' + name + ' [name="betInfo.minStake"]').rules("add", {
			required : true,
			number : true,
			maxlength : 15,
			maxTo : '#' + name + ' [id="txtBetHouseStake"]',
			min : 0,
			messages : {
				required : message_bet_minstake_required,
				number : message_bet_minstake_number,
				maxlength : message_bet_minstake_maxlength,
				maxTo : message_bet_minstake_maxto,
				min : message_bet_minstake_min
			}
		});
		$('#' + name + ' [name="betInfo.stopDate"]').rules("add", {
			required : true,
			datetime : true,
			maxTo : '#hidEventEndDate',
			messages : {
				required : message_bet_stopdate_required,
				datetime : message_bet_stopdate_datetime,
				maxTo : message_bet_stopdate_maxto
			}
		});
	});
	return true;
});

function validatePossibilities() {
	$('form[id^="frmAddBet_"]').each(function() {
		var name = $(this).attr('name');
		$('#' + name + ' [name^="betInfo.betPosInfoList["][name$="].active"]').each(function() {
			$(this).rules("add", {
				atLeastCheck : name,
				messages : {
					atLeastCheck : message_bet_betpos_required
				}
			});
		});
	});
}

function validateOddValue(form,target) {
	var name = $(target).attr('name');
	var oddName = name.replace('active', 'oddValue');
	if ($(target).is(':checked')) {
		$('#' + form + ' [name="' + oddName + '"]').rules("add", {
			required : true,
			number : true,
			maxlength : 15,
			min : 1.01,
			messages : {
				required : message_bet_betpos_odd_required,
				number : message_bet_betpos_odd_number,
				maxlength : message_bet_betpos_odd_maxlength,
				min : message_bet_betpos_odd_min
			}
		});
	} else {
		$('#' + form + ' [name="' + oddName + '"]').rules("remove");
	}
}

function _uploadSuccess(responseText, statusText) {
	$('#imgEvent').attr('src', ROOT_IMAGE_URL + responseText.resource.uri);
	$('form[id^="frmAddBet_"]').each(function() {
		var name = $(this).attr('name');
		$('#' + name + ' [id=_imagePath]').val(responseText.resource.uri);
	});
}

function toggle(source) {
	var div1 = document.getElementById(source)
	if (div1.style.display == 'none') {
		div1.style.display = ''
	} else {
		div1.style.display = 'none'
	}
}

function show(source) {
	var div1 = document.getElementById(source)
	if (div1.style.display == 'none') {
		div1.style.display = ''
	}
}

function hide(source) {
	var div1 = document.getElementById(source)
	div1.style.display = 'none'
}

function doChangeBetType(form) {
	$('form[id^="frmAddBet_"]').each(function() {
		$(this).hide();
	});
	$('#' + form).show();
	$('#' + form + ' [id=txtBetName]').focus();
}

function lockForm(value) {
	$('form[id^="frmAddBet_"]').each(function() {
		var name = $(this).attr('name');
		var optId = 'opt' + name.replace('frmAddBet_', '');
		//BettingExchange
		$('#' + optId).attr('disabled', value);
		$('#' + name + ' [id=txtBetName]').attr('disabled', value);
		$('#' + name + ' [id=txtBetDescription]').attr("disabled", value);
		if($('#' + name + ' [id=txtBetHouseStake]').length > 0) {
			$('#' + name + ' [id=txtBetHouseStake]').attr("disabled", value);
		}
		$('#' + name + ' [id=txtBetMinStake]').attr("disabled", value);
		$('#' + name + ' [id=txtBetStopDate]').attr("disabled", value);
		$('#' + name + ' [id=cmdSubmit]').attr("disabled", value);
		$('#' + name + ' [id=cmdCancel]').attr("disabled", value);
	});
}

function doSubmit(frmAddBet) {
	$('#' + frmAddBet).submit();
}

var color = "white";
function __swap(obj) {
	color = obj.style.backgroundColor;
	obj.style.backgroundColor = '#eeeeee';
}
function __reSwap(obj) {
	obj.style.backgroundColor = color;
}
function scrollbar() {
	var scrollBorderColor = "#7F8E91";
	var scrollFaceColor = "#F0F6FF";
	with (document.body.style) {
		scrollbarDarkShadowColor = scrollBorderColor;
		scrollbar3dLightColor = scrollBorderColor;
		scrollbarArrowColor = "black";
		scrollbarBaseColor = scrollFaceColor;
		scrollbarFaceColor = scrollFaceColor;
		scrollbarHighlightColor = scrollFaceColor;
		scrollbarShadowColor = scrollFaceColor;
		scrollbarTrackColor = "white";
	}
}
function swapborder(obj) {
	obj.style.border = "0.2cm groove pink";
}
function reSwapborder(obj) {
	obj.style.border = "red";
}
function swap(obj) {
	obj.style.background = "#C9E9FC";
}
function reSwap(obj) {
	obj.style.background = "white";
}